package com.svs.quartz.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.svs.common.utils.data.JDBCTemplateUtil;
import com.svs.framework.base.service.CrudService;
import com.svs.quartz.domain.SyncEntity;
import com.svs.quartz.domain.WisDataCol;
import com.svs.quartz.domain.WisDataGroup;
import com.svs.quartz.domain.WisDataSource;
import com.svs.quartz.helper.WisDataGroupHelper;
import com.svs.quartz.mapper.master.WisDataColDao;
import com.svs.quartz.mapper.master.WisDataGroupDao;
import com.svs.quartz.mapper.master.WisDataSourceDao;

/**
 * 数据同步字段 服务层实现
 * 
 * @author 杜建桥
 * @date 2019-02-16
 */
@Service
public class WisDataColService extends CrudService<WisDataColDao, WisDataCol> {

	@Autowired
	WisDataColDao wisDataColDao;
	@Autowired
	WisDataGroupDao wisDataGroupDao;
	@Autowired
	WisDataSourceDao wisDataSourceDao;

	/**
	 * 执行同步
	 * 
	 * @param id
	 */
	public void exeSyncData(Long id) {
		WisDataGroup wisDataGroup = wisDataGroupDao.selectByPrimaryKey(id);
		WisDataSource wisDataSource = wisDataSourceDao.selectByPrimaryKey(wisDataGroup.getSourceaId());
		WisDataSource wisDatabSource = wisDataSourceDao.selectByPrimaryKey(wisDataGroup.getSourcebId());
		WisDataCol col = new WisDataCol();
		col.setGroupId(id);
		List<WisDataCol> wisDataCols = wisDataColDao.selectAllList(col);
		JdbcTemplate jdTemplatea = JDBCTemplateUtil.createJDBCTemplate(wisDataSource.getDBLink());
		JdbcTemplate jdTemplateb = JDBCTemplateUtil.createJDBCTemplate(wisDatabSource.getDBLink());
		int len = null != wisDataCols ? wisDataCols.size() : 0 ;
		WisDataCol linkCol = null ;
		for(int i=0 ;i<len;i++){
			WisDataCol wisDataCol = wisDataCols.get(i) ;
			if(WisDataGroupHelper.LINKED_COL.equals(wisDataCol.getLinkedCol())){
				linkCol = wisDataCol ;
			}
		}
		
		SyncEntity syncEntity = new SyncEntity() ;
		syncEntity.setWisDataGroup(wisDataGroup);
		syncEntity.setWisDataSource(wisDataSource);
		syncEntity.setTargetDataSource(wisDatabSource);
		syncEntity.setWisDataCols(wisDataCols);
		syncEntity.setJdTemplate(jdTemplatea);
		syncEntity.setTargetJdbcTemplate(jdTemplateb);
		syncEntity.setLinkCol(linkCol);
		syncEntity.setLen(len);
		
		// 同步策略 ，同步表写入目标表
		if (WisDataGroupHelper.SYNCWAY_0.equals(wisDataGroup.getSyncWay())) {
			updateTarget(syncEntity) ;
		}
		// 同步策略 ，目标表写入同步表
		if (WisDataGroupHelper.SYNCWAY_1.equals(wisDataGroup.getSyncWay())) {
			updateSync(syncEntity) ;
		} 
		// 同步策略 ，目标表写入同步表
		if (WisDataGroupHelper.SYNCWAY_2.equals(wisDataGroup.getSyncWay())) {
			updateTarget(syncEntity) ;
			updateSync(syncEntity) ;
		} 
		
	}

	/**
	 * 更新同步表数据
	 * 
	 * @param syncEntity
	 */
	private void updateSync(SyncEntity syncEntity) {
		List<Map<String, Object>> resultList = syncEntity.getTargetJdbcTemplate().queryForList(syncEntity.getSelectTargetSql()) ;
		int len = null != resultList ? resultList.size() : 0 ;
		String select = syncEntity.getSelectSql() ;
		WisDataCol linkCol = syncEntity.getLinkCol() ;
		for(int i=0;i<len;i++ ) {
			Map<String, Object> result = resultList.get(i) ;
			String chkSelect = select ;
			if(null != linkCol) {
				chkSelect += " and "+linkCol.getColName() +" = '"+result.get(linkCol.getTargetCol())+"' ;" ;
				List<Map<String, Object>> results = syncEntity.getJdTemplate().queryForList(chkSelect) ;
				if(null != results && !results.isEmpty()) {
					continue ;
				}
				syncEntity.getJdTemplate().execute(syncEntity.getInsertSql(result));
			}
		}
	}

	/**
	 * 更新目标表
	 * 
	 * @param syncEntity
	 */
	private void updateTarget(SyncEntity syncEntity) {
		List<Map<String, Object>> resultList = syncEntity.getJdTemplate().queryForList(syncEntity.getSelectSql()) ;
		int len = null != resultList ? resultList.size() : 0 ;
		String select = syncEntity.getSelectTargetSql() ;
		WisDataCol linkCol = syncEntity.getLinkCol() ;
		for(int i=0;i<len;i++ ) {
			Map<String, Object> result = resultList.get(i) ;
			String chkSelect = select ;
			if(null != linkCol) {
				chkSelect += " and "+linkCol.getTargetCol() +" = '"+result.get(linkCol.getColName())+"' ;" ;
				List<Map<String, Object>> results = syncEntity.getTargetJdbcTemplate().queryForList(chkSelect) ;
				if(null != results && !results.isEmpty()) {
					continue ;
				}
				syncEntity.getTargetJdbcTemplate().execute(syncEntity.getInsertTargetSql(result));
			}
		}
		
	}

}
